<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="数据日期">
              <j-date placeholder="请选择数据日期" v-model="queryParam.reporttaskdate"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="处理状态">
              <j-dict-select-tag placeholder="请选择处理状态" v-model="queryParam.processingStatus" dictCode="processingStatus"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="反馈日期">
                <j-date placeholder="请选择反馈日期" v-model="queryParam.createTimeStr"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="反馈报文类型">
                <j-dict-select-tag placeholder="请选择反馈报文类型" v-model="queryParam.reportType" dictCode="feedback_report_type"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>

              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>

            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="downLoadErrorDesc()">错误码导出</a-button>
      <a-button type="primary" icon="download" @click="downLoadErrorDetail()">报表明细导出</a-button>
<!--
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('反馈报文')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
-->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
<!--
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
-->
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)" >查看错误码</a>
          <a-divider type="vertical" v-has="'feedback:feedbackFilesDown'"/>
          <a @click="feedbackFilesDown(record)" v-has="'feedback:feedbackFilesDown'">反馈源文件下载</a>
          <a-divider type="vertical" v-has="'feedback:viewFeedbackDetail'"/>
          <a @click="viewFeedbackDetail(record)" v-if="record.reportType=='rf' || record.reportType=='ch'" v-has="'feedback:viewFeedbackDetail'">查看报表明细</a>
          <span v-if="record.reportType=='rr' || record.reportType=='rl'" v-has="'feedback:viewFeedbackDetail'">查看报表明细</span>
          <a-divider type="vertical" v-has="'feedback:handleBackInfo'"/>
          <a @click="handleBackInfo(record)" v-has="'feedback:handleBackInfo'">打回重报</a>
          <a-divider type="vertical" v-has="'feedback:feedbackHandle'"/>
          <a v-if="record.processingStatus != '1'" @click="feedbackHandle(record)" v-has="'feedback:feedbackHandle'">处理</a>
          <span v-if="record.processingStatus == '1'" v-has="'feedback:feedbackHandle'">处理</span>
<!--
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
-->
        </span>

      </a-table>
    </div>

    <llbb-feedback-message-modal ref="modalForm" @ok="modalFormOk"></llbb-feedback-message-modal>
    <ViewDetailModal ref="viewBackDetail" @ok="modalFormOk"></ViewDetailModal>
    <HandelBackModal ref="handelBack" @ok="modalFormOk"></HandelBackModal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {getAction} from '@/api/manage'
  import Vue from 'vue'
  import axios from 'axios';
  Vue.prototype.$axios = axios;
  import { ACCESS_TOKEN} from "@/store/mutation-types"
  import LlbbFeedbackMessageModal from './modules/LlbbFeedbackMessageModal'
  import ViewDetailModal from './detail/ViewDetailModal.vue'
  import HandelBackModal from './backInfo/HandelBackModal.vue'

  export default {
    name: 'LlbbFeedbackMessageList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      LlbbFeedbackMessageModal,
      ViewDetailModal,
      HandelBackModal,
    },
    data () {
      return {
        description: '反馈报文管理页面',
        // 表头
        columns: [
          {
            title: '选项',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'报文名称',
            align:"center",
            dataIndex: 'reportid'
          },
          {
            title:'数据日期',
            align:"center",
            dataIndex: 'reporttaskdate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'反馈日期',
            align:"center",
            dataIndex: 'createTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'错误条数',
            align:"center",
            dataIndex: 'errorNum'
          },
          {
            title:'错误报表',
            align:"center",
            dataIndex: 'reporttypecode'
          },
          {
            title:'反馈文件',
            align:"center",
            dataIndex: 'repotfilename',
          },
          {
            title:'金融机构编码',
            align:"center",
            dataIndex: 'bankcode'
          },
          /*{
            title:'行内机构号',
            align:"center",
            dataIndex: 'innercode'
          },*/
          {
            title:'反馈报文类型',
            align:"center",
            dataIndex: 'reportType_dictText'
          },
          {
            title:'反馈来源',
            align:"center",
            dataIndex: 'feedbackSources_dictText'
          },
          /*{
            title:'反馈状态',
            align:"center",
            dataIndex: 'reponsestate_dictText'
          },
          {
            title:'成功失败',
            align:"center",
            dataIndex: 'issuccess_dictText'
          },*/
/*
          {
            title:'信息描述',
            align:"center",
            dataIndex: 'reponsemsg'
          },
*/
          {
            title:'处理状态',
            align:"center",
            dataIndex: 'processingStatus_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/feedbackMessage/llbbFeedbackMessage/list",
          delete: "/feedbackMessage/llbbFeedbackMessage/delete",
          deleteBatch: "/feedbackMessage/llbbFeedbackMessage/deleteBatch",
          exportXlsUrl: "/feedbackMessage/llbbFeedbackMessage/exportXls",
          importExcelUrl: "feedbackMessage/llbbFeedbackMessage/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'reportid',text:'报送ID'})
        fieldList.push({type:'date',value:'reporttaskdate',text:'报送任务日期'})
        fieldList.push({type:'string',value:'repotfilename',text:'报文名称'})
        fieldList.push({type:'string',value:'bankcode',text:'金融机构编码'})
        fieldList.push({type:'string',value:'innercode',text:'行内机构号'})
        fieldList.push({type:'string',value:'reporttypecode',text:'采集报表编码'})
        fieldList.push({type:'string',value:'reponsestate',text:'反馈状态',dictCode:'reponsestate'})
        fieldList.push({type:'string',value:'issuccess',text:'成功失败',dictCode:'issuccess'})
        fieldList.push({type:'string',value:'reponsemsg',text:'信息描述'})
        this.superFieldList = fieldList
      },
      feedbackFilesDown(record){
        //批量下载反馈文件
        //文件名称
        let repotfilename = record.repotfilename;
        if(repotfilename.indexOf(";") != -1){
          //包含
          var fileNames = repotfilename.split(';');
          for(let i=0;i<fileNames.length;i++){
            let fileName = fileNames[i];
            //反馈文件下载
            const that = this;
            let feedbackFilesDownUrl="/feedbackMessage/llbbFeedbackMessage/feedbackFilesDown";
            this.$axios({
              method: "get",
              url: `${window._CONFIG['domianURL']}/${feedbackFilesDownUrl}?repotfilename=`+fileName,
              responseType: "blob",
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                'X-Access-Token':Vue.ls.get(ACCESS_TOKEN)
              },
            }).then(function (response) {
              //下载
              var reader = new FileReader();
              reader.onload = (e) => {
                console.log("开始下载");
                //下载
                let data = response.data;
                if (!data) {
                  return;
                }
                var blob = new Blob([data], {
                  type: "text/plain;charset=UTF-8",
                });
                var url = window.URL.createObjectURL(blob);
                var aLink = document.createElement("a");
                aLink.style.display = "none";
                aLink.href = url;
                aLink.setAttribute("download", fileName);
                document.body.appendChild(aLink);
                aLink.click();
                //下载完成移除元素
                document.body.removeChild(aLink);
                //释放掉blob对象
                window.URL.revokeObjectURL(url);
              };
              reader.readAsText(response.data);
            }).catch(function (error) {
              console.log(error);
            });
          }
        }else{
          //不包含
          //反馈文件下载
          const that = this;
          let feedbackFilesDownUrl="/feedbackMessage/llbbFeedbackMessage/feedbackFilesDown";
          this.$axios({
            method: "get",
            url: `${window._CONFIG['domianURL']}/${feedbackFilesDownUrl}?repotfilename=`+repotfilename,
            responseType: "blob",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
              'X-Access-Token':Vue.ls.get(ACCESS_TOKEN)
            },
          }).then(function (response) {
            //下载
            var reader = new FileReader();
            reader.onload = (e) => {
              console.log("开始下载");
              //下载
              let data = response.data;
              if (!data) {
                return;
              }
              var blob = new Blob([data], {
                type: "text/plain;charset=UTF-8",
              });
              var url = window.URL.createObjectURL(blob);
              var aLink = document.createElement("a");
              aLink.style.display = "none";
              aLink.href = url;
              aLink.setAttribute("download", repotfilename);
              document.body.appendChild(aLink);
              aLink.click();
              //下载完成移除元素
              document.body.removeChild(aLink);
              //释放掉blob对象
              window.URL.revokeObjectURL(url);
            };
            reader.readAsText(response.data);
          }).catch(function (error) {
            console.log(error);
          });
        }
      },
      feedbackHandle(record){
        //处理
        let feedbackHandleUrl="/feedbackMessage/llbbFeedbackMessage/feedbackHandle";
        const that = this;
        this.loading = true;
        getAction(feedbackHandleUrl,{id : record.id}).then((res)=>{
          that.onClearSelected();
          that.loading = false;
          if(res.success){
            that.$message.success(res.message);
            that.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
        });
      },
      viewFeedbackDetail(record){
        //查看反馈明细
        this.$refs.viewBackDetail.title = "反馈报文明细";
        this.$refs.viewBackDetail.show(record);
        this.$refs.viewBackDetail.disableSubmit = true;
      },
      handleBackInfo(record){
        //打回重报
        this.$refs.handelBack.title = "打回重报";
        this.$refs.handelBack.show(record);
        this.$refs.handelBack.disableSubmit = true;
      },
      downLoadErrorDesc(){
        //信息描述导出
        if(this.selectedRowKeys.length==0){
          this.$message.warning("请选择你要批量下载的数据");
          return false;
        }
        var ids = "";
        for(var i=0;i<this.selectedRowKeys.length;i++){
          ids += this.selectedRowKeys[i] + ",";
        }
        this.$axios({
          method: "get",
          url: `${window._CONFIG['domianURL']}/feedbackMessage/llbbFeedbackMessage/downLoadErrorDesc?ids=`+ids,
          responseType: "blob",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'X-Access-Token':Vue.ls.get(ACCESS_TOKEN)
          },
        }).then(function (response) {
          //下载
          var reader = new FileReader();
          reader.onload = (e) => {
            console.log("开始下载");
            //下载
            let data = response.data;
            if (!data) {
              return;
            }
            var blob = new Blob([data], {
              type: "text/plain;charset=UTF-8",
            });
            var url = window.URL.createObjectURL(blob);
            var aLink = document.createElement("a");
            aLink.style.display = "none";
            aLink.href = url;
            aLink.setAttribute("download", "反馈错误信息描述.txt");
            document.body.appendChild(aLink);
            aLink.click();
            //下载完成移除元素
            document.body.removeChild(aLink);
            //释放掉blob对象
            window.URL.revokeObjectURL(url);
          };
          reader.readAsText(response.data);
        }).catch(function (error) {
            console.log(error);
        });
        
      },
      downLoadErrorDetail(){
        //反馈明细导出
        if(this.selectedRowKeys.length==0){
          this.$message.warning("请选择你要批量下载的数据");
          return false;
        }
        var ids = "";
        for(var i=0;i<this.selectedRowKeys.length;i++){
          ids += this.selectedRowKeys[i] + ",";
        }
        //下载反馈错误信息
        this.$axios({
          method: "get",
          url: `${window._CONFIG['domianURL']}/feedbackMessage/llbbFeedbackMessage/downLoadErrorDetail?ids=`+ids,
          responseType: "blob",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'X-Access-Token':Vue.ls.get(ACCESS_TOKEN)
          },
        }).then(function (response) {
          //下载
          var reader = new FileReader();
          reader.onload = (e) => {
            console.log("开始下载");
            //下载
            let data = response.data;
            if (!data) {
              return;
            }
            var blob = new Blob([data], {
              type: "text/plain;charset=UTF-8",
            });
            var url = window.URL.createObjectURL(blob);
            var aLink = document.createElement("a");
            aLink.style.display = "none";
            aLink.href = url;
            aLink.setAttribute("download", "反馈报文明细.txt");
            document.body.appendChild(aLink);
            aLink.click();
            //下载完成移除元素
            document.body.removeChild(aLink);
            //释放掉blob对象
            window.URL.revokeObjectURL(url);
          };
          reader.readAsText(response.data);
        }).catch(function (error) {
            console.log(error);
        });
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>